﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace EDA_TP_5847
{
    /// <summary>   2º Formulario. </summary>
    ///
    /// <remarks>   Andre, 11/06/2013. </remarks>
    public partial class Form2 : Form
    {
        public string algoritmo;
        public List<Dictionary<int, double>> temposExecucao;

        /// <summary>   Constructor. </summary>
        /// <param name="todosTempos">  todos tempos. </param>
        /// <param name="algoritmo">    o algoritmo. </param>
        public Form2(List<Dictionary<int, double>> todosTempos, string algoritmo)
        {
            this.temposExecucao = todosTempos;
            this.algoritmo = algoritmo;
            InitializeComponent();
        }

        /// <summary>   chama o Evento. É chamado quando o Formulario está a carregar. </summary>
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Informacao do Evento. </param>
        private void Form2_Load(object sender, EventArgs e)
        {
            this.Text = "Estatisticas - " + algoritmo;
            this.label1.Text = algoritmo;
            this.chart1.Titles.Add("Tempos de Execução");
            Random randonGen = new Random();

            int i = 1;
            foreach (Dictionary<int, double> tempos in temposExecucao)
            {
                this.chart1.Series.Add(i + "ª Execução");
                Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
                chart1.Series[i + "ª Execução"].Color = randomColor;
                foreach (KeyValuePair<int, double> ponto in tempos)
                {
                    chart1.Series[i + "ª Execução"].Points.Add(new DataPoint(ponto.Key, ponto.Value));
                }
                chart1.Series[i + "ª Execução"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                this.chart1.Series[i + "ª Execução"].Enabled = false;
                i++;
            }

            this.chart1.Series.Add("Média");
            chart1.Series["Média"].Color = Color.Red;
            foreach (KeyValuePair<int, double> ponto in this.getListaMedias())
            {
                chart1.Series["Média"].Points.Add(new DataPoint(ponto.Key, ponto.Value));
                this.dataGridView1.Rows.Add(ponto.Key, ponto.Value);
            }
            chart1.Series["Média"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            this.chart1.Series["Média"].Enabled = true;

            for (int x = 0; x < temposExecucao[0].Count; x++)
            {
                List<double> tempos = new List<double>();
                for (int y = 0; y < temposExecucao.Count; y++)
                {
                    tempos.Add(temposExecucao[y].Values.ToArray()[x]);
                }
                double variancia = this.variance(tempos, this.getListaMedias().Values.ToList()[x]);
                double desvPadrao = Math.Sqrt(variancia);
                this.dataGridView1.Rows[x].Cells[2].Value = variancia;
                this.dataGridView1.Rows[x].Cells[3].Value = desvPadrao;
            }
        }

        /// <summary>   obtem a lista das medias. </summary>
        /// <returns>   A lista das medias. </returns>
        private Dictionary<int, double> getListaMedias()
        {
            Dictionary<int, double> listaMedias = new Dictionary<int, double>();
            for (int i = 0; i < temposExecucao[0].Count; i++)
            {
                double somaMedia = 0;
                for (int y = 0; y < temposExecucao.Count; y++)
                {
                    somaMedia += temposExecucao[y].Values.ToArray()[i];
                }
                listaMedias.Add(temposExecucao[0].Keys.ToArray()[i], somaMedia / temposExecucao.Count);
            }
            return listaMedias;
        }

        /// <summary>   Event handler. Chamado quando a checkbox1 é modificada </summary>
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Informação do Evento. </param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this.chart1.Series["Média"].Enabled = true;
            }
            else
            {
                this.chart1.Series["Média"].Enabled = false;
            }
        }

        /// <summary>   Event handler. Chamado quando a checkbox2 é modificada</summary>
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Informacao do Evento. </param>
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                for (int i = 1; i <= temposExecucao.Count; i++)
                {
                    this.chart1.Series[i + "ª Execução"].Enabled = true;
                }
            }
            else
            {
                for (int i = 1; i <= temposExecucao.Count; i++)
                {
                    this.chart1.Series[i + "ª Execução"].Enabled = false;
                }
            }
        }

        /// <summary>   Obtem a Varianca. </summary>
        /// <param name="values">   Lista de valores. </param>
        /// <param name="mean">     A Media. </param>
        ///
        /// <returns>   A variancia </returns>
        private double variance(List<double> values, double mean)
        {
            double variance = 0;

            for (int i = 0; i < values.Count; i++)
            {
                variance += Math.Pow((values[i] - mean), 2);
            }

            variance /= values.Count;
            return variance;
        }
    }
}
